package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.TopicCreateVo;
import com.cskaoyan.bean.vo.TopicReadVo;
import com.cskaoyan.exception.ValidationException;
import com.cskaoyan.service.TopticService;
import com.cskaoyan.utils.ValidationUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

/**
 * 专题管理
 * @author Fyy
 * @since 2022/12/29 22:40
 */

@RestController
@RequestMapping("admin/topic")
public class TopicController {
    @Autowired
    TopticService topticService;

    /**
     *专题管理页面
     * @param basePageInfo
     * @param title
     * @param subtitle
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo, String title, String subtitle) {
        CommonData<MarketTopic> list = topticService.list(basePageInfo, title, subtitle);
        return BaseRespVo.ok(list, "成功");
    }

    /**
     * 新建专题
     * @param marketTopic
     * @param bindingResult
     * @return
     * @throws ValidationException
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody @Valid MarketTopic marketTopic, BindingResult bindingResult) throws ValidationException {
        //参数校验
        ValidationUtil.valid(bindingResult);
        //添加日期
        marketTopic.setAddTime(new Date());
        //修改日期
        marketTopic.setUpdateTime(new Date());
        topticService.create(marketTopic);
        TopicCreateVo topicCreateVo = new TopicCreateVo();
        BeanUtils.copyProperties(marketTopic,topicCreateVo);
        return BaseRespVo.ok(topicCreateVo,"成功");
    }

    /**
     * 修改专题
     * @param marketTopic
     * @param bindingResult
     * @return
     * @throws ValidationException
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody @Valid MarketTopic marketTopic, BindingResult bindingResult) throws ValidationException {
        //参数校验
        ValidationUtil.valid(bindingResult);
        //修改日期
        marketTopic.setUpdateTime(new Date());
        topticService.update(marketTopic);
        return BaseRespVo.ok(marketTopic,"成功");
    }

    /**
     * 删除专题
     * @param marketTopic
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketTopic marketTopic){
        topticService.delete(marketTopic);
        return BaseRespVo.ok("成功");
    }

    /**
     * 编辑专题页面数据
     * @param id
     * @return
     */
    @RequestMapping("read")
    public BaseRespVo read(Integer id){
        TopicReadVo readVo = topticService.read(id);
        return BaseRespVo.ok(readVo,"成功");
    }
}
